package com.demo2do.fms.admin.web.controller.report;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.report.DriverAccumulatedMileageReportSearch;
import com.demo2do.fms.admin.entity.report.report.DriverCostReportSearch;
import com.demo2do.fms.admin.entity.report.report.TransportRevenueScheduleReportSearch;
import com.demo2do.fms.admin.entity.report.report.VehicleAccumulatedMileageReportSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.authority.OrganizationService;
import com.demo2do.fms.admin.service.report.ReportService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.authority.Organization;

/**
 * 运输报表管理
 * 
 * @author Wy
 *        
 */
@Controller
@RequestMapping("/reports")
@MenuSetting("menu-report")
public class ReportController {
    
    private static final Logger logger = LoggerFactory.getLogger(ReportController.class);
    
    @Autowired
    private ReportService reportService;
                                
    @Autowired
    private OrganizationService organizationService;
    
    /**
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String home() {
        return "report/report-home";
    }
                                
    /**
     * 驳运车累计行驶公里数查询 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/vehicle-accumulated-mileage-search", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle-accumulated-mileage")
    public ModelAndView vehicleAccumulatedMileageSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/report/vehicle-accumulated-mileage-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 驳运车累计行驶公里数查询
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "vehicle-accumulated-mileage")
    @MenuSetting("submenu-vehicle-accumulated-mileage")
    public ModelAndView onVehicleAccumulatedMileageSearch(VehicleAccumulatedMileageReportSearch search,
                                                          HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/report/vehicle-accumulated-mileage-list");
        
        int totals = reportService.count(search);
        if (totals > 0) {
            
            logger.warn("#onVehicleAccumulatedMileageSearch() - search size=[{}]", totals);
            
            List<Map<String, Object>> result = reportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 驳运车累计行驶公里数导出
     * 
     * @param search
     * @return
     */
    @RequestMapping(value = "/vehicle-accumulated-mileage-export", method = RequestMethod.GET)
    public ModelAndView vehicleAccmulatedMileageExport(VehicleAccumulatedMileageReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/vehicle-accumulated-mileage-export.xlsx"));

        // 1. list result 
        int totals = reportService.count(search); 
        if (totals > 0) {
            List<Map<String, Object>> results = reportService.search(search, page.with(totals));
            modelAndView.addObject("results", results);
        }
        
        // 2. get organization
        Organization organization = organizationService.get(Organization.class, search.getOrganizationId());
        String organizationName = "上海" + organization.getName() + "轿车运输有限公司";
        modelAndView.addObject("organizationName", organizationName);
        
        // 3. format_util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员累计行驶公里数查询 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/driver-accumulated-mileage-search", method = RequestMethod.GET)
    @MenuSetting("submenu-driver-accumulated-mileage")
    public ModelAndView driverAccumulatedMileageSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/report/driver-accumulated-mileage-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员累计行驶公里数查询
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "driver-accumulated-mileage")
    @MenuSetting("submenu-driver-accumulated-mileage")
    public ModelAndView onDriverAccumulatedMileageSearch(DriverAccumulatedMileageReportSearch search,
                                                         HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/report/driver-accumulated-mileage-list");
        
        int totals = reportService.count(search);
        if ( totals > 0 ) {
            
            List<Map<String, Object>> result = reportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员累计行驶公里数导出
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/driver-accumulated-mileage-export", method = RequestMethod.GET, params = "driver-accumulated-mileage")
    @MenuSetting("submenu-driver-accumulated-mileage")
    public ModelAndView driverAccumulatedMileageExport(DriverAccumulatedMileageReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/driver-accumulated-mileage-export.xls"));
        
        // 1. list driver distance
        int totals = reportService.count(search); 
        if (totals > 0) {
            
            List<Map<String, Object>> results = reportService.search(search, page.with(totals));
            modelAndView.addObject("results", results);
            
        }
        
        // 2. organization
        Organization organization = organizationService.load(Organization.class, search.getOrganizationId());
        modelAndView.addObject("organization", organization);
        
        // 3. fmt_util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 运输收入明细表 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/transport-revenue-schedule-search", method = RequestMethod.GET)
    @MenuSetting("submenu-transport-revenue-schedule")
    public ModelAndView transportRevenueScheduleSearch(@Secure Login login) {
        
		ModelAndView modelAndView = new ModelAndView("report/report/transport-revenue-schedule-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 运输收入明细表 - search
     * 
     * @param search
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, params = "transport-revenue-schedule")
    @MenuSetting("submenu-transport-revenue-schedule")
    public ModelAndView onTransportRevenueScheduleSearch(TransportRevenueScheduleReportSearch search,
                                                         HttpServletRequest request, Page page) {
            
        ModelAndView modelAndView = new ModelAndView("report/report/transport-revenue-schedule-list");
        
        int totals = reportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> result = reportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            modelAndView.addObject("reportSearch", search);
        }
        
        return modelAndView;
        
    }
    
    /**
     * 运输收入明细表 - 导出所有记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/transport-revenue-schedule-export", method = RequestMethod.POST)
    public ModelAndView transportRevenueScheduleExport(TransportRevenueScheduleReportSearch search, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/transport-revenue-schedule-export.xlsx"));
        
        // 1. list driver distance
        List<Map<String, Object>> results = reportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. organization
        Organization organization = organizationService.load(Organization.class, search.getOrganizationId());
        modelAndView.addObject("organization", organization);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员报销信息统计 - panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/driver-cost-search", method = RequestMethod.GET)
    @MenuSetting("submenu-driver-cost")
    public ModelAndView driverCostSearch(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/report/driver-cost-search");
        
        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员报销信息统计
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "driver-cost")
    @MenuSetting("submenu-driver-cost")
    public ModelAndView onDriverCostSearch(DriverCostReportSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/report/driver-cost-list");
        
        int totals = reportService.count(search);
        if (totals > 0) {
            
            List<Map<String, Object>> result = reportService.search(search, page.with(totals));
            modelAndView.addObject("result", result);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 驾驶员报销信息统计 - 输出当前记录/输出所有记录
     * 
     * @param search
     * @param page
     * @return
     */
    @RequestMapping(value = "/driver-cost-export", method = RequestMethod.GET)
    public ModelAndView driverCostExport(DriverCostReportSearch search, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("report/driver-cost-export.xlsx"));
        
        // 1. list driver distance
        List<Map<String, Object>> results = reportService.search(search, page);
        modelAndView.addObject("results", results);
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}
